package com.auto.base;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.apache.http.util.TextUtils;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Listeners;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
 *所有测试用例的父类
 * Created by yidian on 2018/3/21.
 * 微信版本6.6.7
 */
@Listeners({AssertionListener.class})
public class InitAppium {
    //调试设备名字
    public static String deviceName = "8f023638";
    //调试设备系统版本
    public static String platformVersion = "6.0.1";
    //app路径
    public static String appPath = System.getProperty("user.dir") + "/src/main/java/apps/shouhu2.2.4.apk";

    //包名
    public static String appPackage = "com.tencent.mm";

    //是否需要重新安装
    public static String noReset = "True";

    //是否不重新签名
    public static String noSign = "True";

    //是否使用unicode输入法，真是支持中文
    public static String unicodeKeyboard = "True";

    //是否祸福默认呢输入法
    public static String resetKeyboard = "True";

    //要启动的Activity
    //public static String appActivity = appPackage + ".activity.login.WelcomeActivity";
    public static String appActivity = "com.tencent.mm.ui.LauncherUI";

    public  AndroidDriver<AndroidElement> driver = null;



    //构造方法
    public InitAppium() {
        this(new Builder());
    }

    public InitAppium(Builder builder) {
        appActivity = builder.appActivity;
        appPackage = builder.appPackage;
        appPath = builder.appPath;
        deviceName = builder.deviceName;
        noReset = builder.noReset;
        noSign = builder.noSign;
        unicodeKeyboard = builder.unicodeKeyboard;
        resetKeyboard = builder.resetKeyboard;
    }

    /**
     * appium启动参数
     *
     * @throws MalformedURLException
     */
    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
       // capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
        capabilities.setCapability("deviceName", deviceName);
        capabilities.setCapability("platformName", "Android"); //指定测试平台
        capabilities.setCapability("platformVersion", platformVersion);
       // capabilities.setCapability("app", new File(appPath).getAbsolutePath());
        capabilities.setCapability("appPackage", appPackage);
        //支持中文
        capabilities.setCapability("unicodeKeyboard", unicodeKeyboard);
        //运行完毕之后，变回系统的输入法
        capabilities.setCapability("resetKeyboard", resetKeyboard);
        //设备UDID(device)
        //capabilities.setCapability("udid", "8f023638");
        //启动对应的chromedriver
        File chromedriver = new File("E:/chromedriver/chromedriver.exe");
        capabilities.setCapability("chromedriverExecutable", chromedriver.getAbsolutePath());
        //不重复安装
        capabilities.setCapability("noReset", noReset);
        //不重新签名
        capabilities.setCapability("noSign", noSign);

        //重新创建ChromeDriverSessions,解决第二次切换webview的context卡住问题
        capabilities.setCapability("recreateChromeDriverSessions", "True");

        //打开的activity
        if(!TextUtils.isEmpty(appActivity)){
            capabilities.setCapability("appActivity", appActivity);
        }

        //关键是加上这段
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("androidProcess", "com.tencent.mm:tools");
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        //A new session could not be created的解决方法
        capabilities.setCapability("appWaitActivity","com.tencent.mm.ui.LauncherUI");
        //每次启动时覆盖session，否则第二次后运行会报错不能新建session
        capabilities.setCapability("sessionOverride", true);
        System.out.println("App is done before!");
        //启动Driver
        driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

//    /**
//     * 在testng定义的xml根元素里面的所有执行之前启动设备
//     */
//    @BeforeSuite
//    public void initDriver() {
//        try {
//            setUp();
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
//        print("BeforeSuite运行");
//        Assert.assertNotNull(driver);
//    }

    @AfterTest
    public void afterTest() {
        //driver.quit();
    }

    @AfterClass
    public void afterClass(){
        //每一个用例完毕结束这次测试
        driver.quit();
    }
    @AfterSuite
    public void tearDown(){
        //在testng定义的xml根元素里面的所有执行之后运行
       // driver.quit();
    }

    /**
     * 打印字符
     *
     * @param str 要打印的字符
     */
    public <T> void print(T str) {
        if (!TextUtils.isEmpty(String.valueOf(str))) {
            System.out.println(str);
        } else {
            System.out.println("输出了空字符");
        }
    }

}